<?php
declare (strict_types=1);

namespace app\admin\service\site;

use app\admin\logic\site\Template as TemplateLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
use think\annotation\Inject;
use think\db\exception\DbException;
use think\facade\Request;
use think\Response;

/**
 * Class Template
 *
 * @package app\admin\service\site
 */
class Template extends BaseService
{
    /**
     * @Inject()
     *
     * @var TemplateLogic
     */
    protected $templateLogic;

    /**
     * index
     *
     * @throws DbException
     */
    public function index(): Response
    {
        // 模板类型
        $templateType = Request::param('template_type/s');

        $where = [];
        Request::has('title', 'get', true) && $where[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // Request::has('pid', 'get', true) && $where[] = ['pid', '=', Request::get('pid/d')];
        $data = $this->templateLogic->getList($where, $templateType);

        // 定义字段
        $keyList = [
            'title'              => ListItem::make()->title('模板名称'),
            'template_type_text' => ListItem::make()->title('模板类型'),
            'is_used'            => ListItem::make('custom')->title('使用状态'),
            'create_time'        => ListItem::make()->title('创建时间'),
            'update_time'        => ListItem::make()->title('最后更新时间'),
        ];
        // 设置表单
        $list = new PageList;
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 添加模板
     */
    public function created(): Response
    {
        // 模板类型
        $templateType = Request::param('template_type');

        $data = Request::post();
        $this->templateLogic->created($data, $templateType);

        return $this->parseToData([], 1, '新建成功');
    }

    /**
     * 更新模板
     *
     * @param int $id
     * @return Response
     */
    public function updated(int $id): Response
    {
        $data = Request::put();
        $res  = $this->templateLogic->updated($id, $data);

        return $this->parseToData($res->toArray(), 1, '编辑成功');

    }

    /**
     * 删除模板
     *
     * @param int $id
     * @return Response
     */
    public function deleted(int $id): Response
    {
        $this->templateLogic->deleted($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 更新字段
     *
     * @param int    $id
     * @param string $field
     * @return Response
     */
    public function updatedBy(int $id, string $field): Response
    {
        $value = Request::put($field);
        $this->templateLogic->updatedBy($id, $field, $value);

        return $this->parseToData([], 1, '操作成功');
    }

    /**
     * 保存模板配置
     *
     * @param int $id
     * @return Response
     * @throws JsonException
     */
    public function saveData(int $id): Response
    {
        $value = Request::post('template_data');
        $this->templateLogic->updatedBy($id, 'template_data', Json::decode($value, 1));

        $publish = Request::post('publish');
        if ($publish) {
            $this->templateLogic->updatedBy($id, 'is_used', 1);
        }

        return $this->parseToData([], 1, '保存成功');
    }

    /**
     * 获取模板配置
     *
     * @param int $id
     * @return Response
     */
    public function getData(int $id): Response
    {
        $template = $this->templateLogic->getInfoById($id);
        $value    = $template->getAttr('template_data');

        return $this->parseToData($value, 1, '获取成功');
    }

}
